/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package VietDQ;

import Entity.Category;
import Entity.Document;
import Entity.Event;
import Entity.Faq;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author dukynguyen
 */
@Stateless
public class VietDQSessionBean implements VietDQSessionBeanLocal {
    @PersistenceContext(unitName = "Group05_NationalLevelPaperPresentation-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public Entity.Faq[] getFAQ() {
        try {
            Query query = em.createNamedQuery("Faq.findAll");
            Collection empList = query.getResultList();
            Faq[] faq = new Faq[empList.size()];
            empList.toArray(faq);
            return faq;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean AddNewFAQ(String question, String answer) {
        boolean check = false;
        try {
            Faq objFa = new Faq();
            objFa.setFAQQuestion(question);
            objFa.setFAQAnswer(answer);
            objFa.setIDelete(false);

            persist(objFa);
            check= true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean UpdateFAQ(int id, String question, String answer) {
        boolean check = false;
        try {
            Faq objFa = em.find(Faq.class, id);
            objFa.setFAQQuestion(question);
            objFa.setFAQAnswer(answer);

            persist(objFa);
            check= true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean activeFAQ(int id) {
        boolean check = false;
        try {
            Faq objFa = em.find(Faq.class, id);
            objFa.setIDelete(false);

            persist(objFa);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean unActiveFAQ(int id) {
        boolean check = false;
        try {
            Faq objFa = em.find(Faq.class, id);
            objFa.setIDelete(true);
        
            persist(objFa);
            check = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public Faq findFAQByPrimarykey(int id) {
        try {
            Faq objFa = em.find(Faq.class, id);
            return objFa;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 //==========toi phan Events========================
    public Event findEventByPK(int EvID) {
        try {
            Event objEv = em.find(Event.class, EvID);
            return  objEv;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public boolean InsertEvents(int CaID, String EvName, String Descriptions, String EvImage, String dateStart, String dateEnd) {
       boolean check = false;
        try {

            Event objEv = new Event();
            Category objCa = em.find(Category.class, CaID);
            objEv.setCategory(objCa);
            objEv.setEvName(EvName);
            objEv.setEvDescription(Descriptions);
            objEv.setEvImage(EvImage);
            objEv.setDateStart(dateStart);
            objEv.setDateEnd(dateEnd);
            objEv.setStatus(true);
            objEv.setIDelete(false);
            objEv.setDateCreate(new Date());

            persist(objEv);
            check = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return check;
    }

    public boolean DeleteEvents(int EvID) {
        try {
            Event objEv = em.find(Event.class, EvID);
            objEv.setIDelete(true);
            persist(objEv);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public Entity.Event[] FindEventByLikeName(String EvName) {
        try {
            String strQL ="SELECT e FROM Event e WHERE e.evName LIKE :evName";
            Query query = em.createQuery(strQL);
            query.setParameter("evName", "%"+ EvName +"%");
            List evList = query.getResultList();
            Event[] result = new Event[evList.size()];
            evList.toArray(result);
                return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

  public Entity.Event[] GetAllEvents() {
      try {
            Query query = em.createNamedQuery("Event.findAll");
            List evList = query.getResultList();
            Event[] result = new Event[evList.size()];
            evList.toArray(result);
                return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public boolean ExpireEvents(int EvID) {
        try {
            Event objEv = em.find(Event.class, EvID);
            objEv.setStatus(true);
            persist(objEv);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public CategoryEventDTO SearchEventsbyCategory(int CaID) {
        try {
            String strQL ="SELECT c FROM Category c WHERE c.caID = :caID AND c.iDelete = :iDelete";
            Query query = em.createQuery(strQL);
            query.setParameter("caID", CaID);
            query.setParameter("iDelete", true);
            List CaList = query.getResultList();
            CategoryEventDTO objDTO = new CategoryEventDTO();
            for (int i= 0; i < CaList.size(); i++) {
                Category objCa = (Category) CaList.get(i);
                Collection EvColl = objCa.getEventCollection();
                Event[] EvList = new Event[EvColl.size()];
                EvColl.toArray(EvList);

                objDTO.setObjCa(objCa);
                objDTO.setListEvent(EvList);
            }
            return objDTO;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public Entity.Event[] findTop5NewEvent() {
        try {
            Query query = em.createQuery("SELECT e FROM Event e WHERE e.iDelete = :iDelete order by e.dateCreate desc");
            query.setParameter("iDelete", false);
            List evList = query.getResultList();
            Event[] result = new Event[evList.size()];
            evList.toArray(result);
                return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public EventDocumentDTO findDocumentByEvent(int id) {
        try {
            Query query = em.createQuery("SELECT e FROM Event e WHERE e.evID = :evID and e.iDelete = :iDelete");
            query.setParameter("evID", id);
            query.setParameter("iDelete", false);
            List listTech =query.getResultList();
            EventDocumentDTO objDTO = new EventDocumentDTO();
            for (int i= 0; i < listTech.size(); i++) {
              Event objT = (Event)listTech.get(i);
              Collection eventComlist = objT.getDocumentCollection();
              Document[] listDocumment = new Document[eventComlist.size()];
              eventComlist.toArray(listDocumment);
              objDTO.setObjE(objT);
              objDTO.setListDoc(listDocumment);
          }
            return objDTO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
}
